<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/15
 * Time: 13:28
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Sailingls extends Base
{
    /**
     * 帆船临时成绩
     * @return mixed|\think\response\Json
     */
    public function index(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->field('match_id, match_lunci')
            ->find();
        $group = Db::table('up_match_apply_group')
            ->join('up_match_group', 'mg_id = mag_group_id', 'left')
            ->where('mag_match_id', $match['match_id'])
            ->field('mg_id, mg_name')
            ->select();
        $lunci = range(1, $match['match_lunci']);
        if(request()->isAjax ()){
            $group_id = input('group_id'); //  组
            $lc_id = input('lc_id'); // 轮次
            $user_real_name = input('user_real_name');
            $where['umd.match_id'] = $match['match_id'];
            $where['umd.flag'] = 3;
            if(isset($group_id) && $group_id != ''){
                $where['umd.level_id'] = $group_id;
            }/*else{
                $where['umd.level_id'] = $group[0]['mg_id'];
            }*/
            if(isset($lc_id) && $lc_id != ''){
                $where['umd.turn'] = $lc_id;
            }/*else{
                $where['umd.turn'] = $lunci[0];
            }*/
            if(isset($user_real_name) && $user_real_name != ''){
                $where['uuu.user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up__match_arrive_record')
                ->alias('umar')
                ->join('up__match_detail umd', 'umar.match_detail_id = umd.id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umar.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $count2 = Db::table('up__match_arrive_record')
                ->alias('umar')
                ->join('up__match_detail umd', 'umar.match_detail_id = umd.id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umar.user_id', 'left')
                ->where($where)
                ->column('umar.id');// 获取总条数
            $lists = Db::table('up__match_arrive_record')
                ->alias('umar')
                ->join('up__match_detail umd', 'umar.match_detail_id = umd.id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umar.user_id', 'left')
                ->field('umar.id, umd.match_id, umar.temp_score, umar.sensor_id, ls_status, umd.turn, 
                uuu.user_phone, user_real_name, user_create_id, uuu.user_id')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('umd.turn asc, temp_score asc')
                ->select();
            foreach($lists as $k => $v){
                // 名次
                $lists[$k]['mc'] = $k + 1;
                // 报名信息
//                if($v['sensor_id']){
//                    $sensor = explode('_', $v['sensor_id']);
//                    $user = Db::table('up_u_user')
//                        ->where('user_id', $sensor[1])
//                        ->field('user_phone, user_real_name, user_create_id')
//                        ->find();
                    $apply = Db::table('up_match_apply_info')
                        ->where('ma_user_id', $v['user_id'])
                        ->where('ma_match_id', $v['match_id'])
                        ->field('ma_fan_code')
                        ->find();
                    $lists[$k]['fan_code'] = $apply['ma_fan_code'];
//                    $lists[$k]['user_phone'] = $user['user_phone'];
//                    $lists[$k]['user_real_name'] = $user['user_real_name'];
//                    $lists[$k]['user_create_id'] = $user['user_create_id'];
//                }
                $lists[$k]['member_type'] = session('cm_member_type');
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        
        $this->assign([
            'group' => $group,
            'lunci' => $lunci,
        ]);
        return $this->fetch();
    }
    
    /**
     * 提交异议
     * @return mixed|\think\response\Json
     */
    public function commitYiyi(){
        $id = input('id');
        if(Request::instance()->isPost()){
            $ls_yiyi_content = input('ls_yiyi_content');
            $update = [
                'ls_yiyi_content' => $ls_yiyi_content,
                'ls_status' => 2,
                'ls_commit_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up__match_arrive_record')->where('id', $id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '提交成功']);
            }else{
                return json(['code' => 100, 'msg' => '提交失败']);
            }
        }
        $this->assign([
            'id' => $id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('sailingls/commityiyi');
        }
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '没有需要导出的数据']);
        }
        $lists = Db::table('up__match_arrive_record')
            ->alias('umar')
            ->join('up__match_detail umd', 'umar.match_detail_id = umd.id', 'left')
            ->join('up_match um', 'umd.match_id = um.match_id', 'left')
            ->join('up_match_group umg', 'umg.mg_id = umd.level_id', 'left')
            ->field('umar.id, umd.match_id, umar.temp_score, umar.sensor_id, ls_status, umd.turn, 
            um.match_title, umg.mg_name')
            ->where('umar.id', 'IN', $ids)
            ->order('umd.turn asc')
            ->select();
        foreach($lists as $k => $v){
            // 名次
            $lists[$k]['mc'] = $k + 1;
            // 报名信息
            $sensor = explode('_', $v['sensor_id']);
            $user = Db::table('up_u_user')
                ->where('user_id', $sensor[1])
                ->field('user_phone, user_real_name, user_create_id')
                ->find();
            $apply = Db::table('up_match_apply_info')
                ->where('ma_user_id', $sensor[1])
                ->where('ma_match_id', $v['match_id'])
                ->field('ma_fan_code')
                ->find();
            // 帆号
            $lists[$k]['fan_code'] = $apply['ma_fan_code'];
            // 真实姓名
            $lists[$k]['user_real_name'] = $user['user_real_name'];
            // 手机号
            $lists[$k]['user_phone'] = "\t".$user['user_phone']."\t";
            // 用户ID
            $lists[$k]['user_create_id'] = "\t".$user['user_create_id']."\t";
        }
        $cellname = [
            ['user_create_id', '用户ID', 15, 'LEFT'],
            ['user_real_name', '姓名', 15, 'LEFT'],
            ['user_phone', '手机号', 20, 'LEFT'],
            ['fan_code', '帆号', 15, 'LEFT'],
            ['temp_score', '成绩', 15, 'LEFT'],
            ['mc', '名次', 15, 'LEFT'],
            ['match_title', '赛事', 20, 'LEFT'],
            ['mg_name', '组别', 20, 'LEFT'],
            ['turn', '轮次', 15, 'LEFT'],
        ];
        $res = exportExcel('临时成绩表', 'lsMark', $cellname, $lists);
        return json($res);
    }
}